﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Scriptor.Presentation.Entity;

namespace Scriptor.Presentation.Templates
{
    public class PresenterTemplate
    {
        List<string> gUsing { get; set; }
        public StringBuilder clausulaUsingFormatada { get; set; }

        List<string> gPresenter { get; set; }
        public StringBuilder clausulaPresenterFormatada { get; set; }

        List<string> gProperty { get; set; }
        List<string> gPropertyAuxiliar { get; set; }
        public StringBuilder propertyPresenterFormatada { get; set; }
        public StringBuilder methodPresenterFormatado { get; set; }

        public string strUsing(string pModulo, string pSubModulo)
        {
            gUsing = new List<string>();

            gUsing.Add("using System;");
            gUsing.Add("using System.Collections.Generic;");
            gUsing.Add("using System.Text;");
            gUsing.Add("using Sinacor.Infra.UI.Common;");
            gUsing.Add(string.Format("using Sinacor.UI.{0}.{1}.Model;",pModulo,pSubModulo));
            gUsing.Add("using System.Collections.ObjectModel;");
            gUsing.Add(string.Format("using Sinacor.UI.{0}.{1}.Common;",pModulo,pSubModulo));
            gUsing.Add(string.Format("using Sinacor.UI.{0}.{1}.Model.Enum;",pModulo,pSubModulo));

            clausulaUsingFormatada = new StringBuilder();

            foreach (string str in gUsing)
            {
                clausulaUsingFormatada.Append(str);
                clausulaUsingFormatada.Append(Environment.NewLine);
            }

            return clausulaUsingFormatada.ToString();
        }

        public string strPresenter(string pNameSpace, string pPresenterName, bool showInheriting)
        {
            gPresenter = new List<string>();

            gPresenter.Add("namespace " + pNameSpace);
            gPresenter.Add("{");
            
            if(showInheriting)
                gPresenter.Add("\tpublic class " + pPresenterName + " : " + "BasePresenter<I" + pPresenterName + ">");
            else
                gPresenter.Add("\tpublic class " + pPresenterName);

            gPresenter.Add("\t{");
            gPresenter.Add("@@property-body");
            gPresenter.Add("@@presenter-body");
            gPresenter.Add("\t}");
            gPresenter.Add("}");

            clausulaPresenterFormatada = new StringBuilder();

            foreach (string str in gPresenter)
            {
                clausulaPresenterFormatada.Append(str);
                clausulaPresenterFormatada.Append(Environment.NewLine);
            }

            return clausulaPresenterFormatada.ToString();
        }

        public string strMethod(Method mtd)
        {
            methodPresenterFormatado = new StringBuilder();
            //Adicionado: 21/8
            if (mtd.MethodComent.Trim().Length > 0)
            {
                methodPresenterFormatado.Append("\t\t/// <summary>" + Environment.NewLine);
                methodPresenterFormatado.Append("\t\t///" + mtd.MethodComent.Replace("\n", "\n\t\t///") + Environment.NewLine);
                methodPresenterFormatado.Append("\t\t/// </summary>" + Environment.NewLine);
            }
            //Fim Adicionado. phsilva
            methodPresenterFormatado.Append("\t\tpublic void " + mtd.MethodName + "()");
            methodPresenterFormatado.Append(Environment.NewLine);
            methodPresenterFormatado.Append("\t\t{");
            methodPresenterFormatado.Append(Environment.NewLine);
            methodPresenterFormatado.Append("\t\t}");
            methodPresenterFormatado.Append(Environment.NewLine);
            methodPresenterFormatado.Append(Environment.NewLine);

            return methodPresenterFormatado.ToString();
        }

        public Method mtdControle { get; set; }
        public bool AppendCommInPresenter;

        public string strProperty(Param param, Method mtd)
        {
            bool CommInPresenter = (string.IsNullOrEmpty(param.ParameterName) && string.IsNullOrEmpty(param.ParameterType)
                && string.IsNullOrEmpty(mtd.MethodName) && string.IsNullOrEmpty(mtd.MethodSignature));

            propertyPresenterFormatada = new StringBuilder();
            gProperty = new List<string>();
            gPropertyAuxiliar = new List<string>();

            gProperty.Add("\t\tprivate Model.@@modelNameModel _@@FirstLetterLower;");                    
            gProperty.Add("\t\tprivate ModelCollection<Model.@@modelNameModel> _@@FirstLetterLowers;");  
            gProperty.Add("\t\tpublic Model.@@modelNameModel @@modelNameModel");                       
            gProperty.Add("\t\tpublic ModelCollection<Model.@@modelNameModel> @@modelNameModels");     
            gProperty.Add("\t\t{");                                               
            gProperty.Add("\t\t\tget { return _@@FirstLetterLower; }");                          
            gProperty.Add("\t\t\tget { return _@@FirstLetterLowers; }");                         
            gProperty.Add("\t\t\tset { _@@FirstLetterLower = value; }");                         
            gProperty.Add("\t\t\tset { _@@FirstLetterLowers = value; }");                        
            gProperty.Add("\t\t}");

            if (!string.IsNullOrEmpty(param.ParameterName))
            {
                //gPropertyAuxiliar.Add("\t\t// Parâmetro Método: " + mtd.MethodName);
                string firstLetterLower = param.ParameterType.Substring(0, 1).ToLower() + param.ParameterType.Substring(1);
                string parameter = param.ParameterType;

                if (param.ParameterIsParameter)
                    parameter = "Parameter." + parameter;

                if (param.ParameterIsList)
                {
                    //Property
                    gPropertyAuxiliar.Add(gProperty[1].Replace("@@modelNameModel", parameter).Replace("@@FirstLetterLower", firstLetterLower));

                    if (param.ParameterIsParameter)
                        gPropertyAuxiliar.Add(gProperty[3].Replace(".@@modelNameModel", "." + parameter).Replace("@@modelNameModel", parameter.Substring(10)));
                    else
                        gPropertyAuxiliar.Add(gProperty[3].Replace("@@modelNameModel", parameter).Replace("@@FirstLetterLower", firstLetterLower));

                    //Abre Chaves
                    gPropertyAuxiliar.Add(gProperty[4]);

                    //get
                    gPropertyAuxiliar.Add(gProperty[6].Replace("@@modelNameModel", parameter).Replace("@@FirstLetterLower", firstLetterLower));

                    //set
                    gPropertyAuxiliar.Add(gProperty[8].Replace("@@modelNameModel", parameter).Replace("@@FirstLetterLower", firstLetterLower));

                    //Fecha Chaves
                    gPropertyAuxiliar.Add(gProperty[9]);
                }
                else
                {
                    //Property
                    gPropertyAuxiliar.Add(gProperty[0].Replace("@@modelNameModel", parameter).Replace("@@FirstLetterLower", firstLetterLower));

                    if (param.ParameterIsParameter)
                        gPropertyAuxiliar.Add(gProperty[2].Replace(".@@modelNameModel", "." + parameter).Replace("@@modelNameModel", parameter.Substring(10)));
                    else
                        gPropertyAuxiliar.Add(gProperty[2].Replace("@@modelNameModel", parameter).Replace("@@FirstLetterLower", firstLetterLower));

                    //Abre Chaves
                    gPropertyAuxiliar.Add(gProperty[4].Replace("@@modelNameModel", parameter).Replace("@@FirstLetterLower", firstLetterLower));

                    //get
                    gPropertyAuxiliar.Add(gProperty[5].Replace("@@modelNameModel", parameter).Replace("@@FirstLetterLower", firstLetterLower));

                    //set
                    gPropertyAuxiliar.Add(gProperty[7].Replace("@@modelNameModel", parameter).Replace("@@FirstLetterLower", firstLetterLower));

                    //Fecha Chaves
                    gPropertyAuxiliar.Add(gProperty[9].Replace("@@modelNameModel", parameter).Replace("@@FirstLetterLower", firstLetterLower));
                }

                gPropertyAuxiliar.Add("");
            }

            if (CommInPresenter && Core.config.showComunicationPropertyInPresenter && !AppendCommInPresenter)
            {
                //Append Communication property in Presenter
                gPropertyAuxiliar = new List<string>();
                string lTemp = "\t\tprivate Communication.{0} _communication{0};";
                gPropertyAuxiliar.Add(
                    String.Format(lTemp,mtd.MethodComent)
                    );
                AppendCommInPresenter = true;

                foreach (string str in gPropertyAuxiliar)
                {
                    propertyPresenterFormatada.Append(str);
                    propertyPresenterFormatada.Append(Environment.NewLine);
                }

                propertyPresenterFormatada.Append(Environment.NewLine);

                this.mtdControle = mtd;

                return propertyPresenterFormatada.ToString().Replace("ms", "ns");
            }
            else
            {
                foreach (string str in gPropertyAuxiliar)
                {
                    propertyPresenterFormatada.Append(str);
                    propertyPresenterFormatada.Append(Environment.NewLine);
                }

                propertyPresenterFormatada.Append(Environment.NewLine);

                this.mtdControle = mtd;

                if (CommInPresenter && AppendCommInPresenter)
                    return string.Empty;
                else
                    return propertyPresenterFormatada.ToString().Replace("ms", "ns");
            }
        }

        public string AppendMethod(string pPresenterScope, StringBuilder stb)
        {
            return pPresenterScope.ToString().Replace("@@presenter-body", stb.ToString());
        }

        public string AppendProperty(string pPresenterScope, StringBuilder stb)
        {
            return pPresenterScope.ToString().Replace("@@property-body", stb.ToString());
        }
    }
}
